#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "qactivemessage.h"
#include "qtactiveobject.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void reportProgress(int v, int max = 100);
    void setResult(long long v);

protected:
    void changeEvent(QEvent *e);

private:
    Ui::MainWindow *ui;
    QtActiveObject aObj;

private slots:
    void on_pushButton_clicked();
};

class CalculateFactorial: public QActiveMessage
{
    Q_OBJECT
public:
    CalculateFactorial(long long v) {
        value = v;
    }
    ~CalculateFactorial() {}

  void Execute()
  {
      long long i = value-1;
      long long r = value;

      int howOften = qMax(int(value / 300), 1); // 300 times max

      while (i>0) {
          r += i;
          --i;
          for(long a = 1; a < 100; ++a) { // havy calculations
              for(long a2 = 1; a2 < 100; ++a2) { // havy calculations
              float f =  a/ 2.23235235235235;
              f *= 444242.332/ a2;
          }}
          if ( (i% howOften) == 0) {
            emit progresChanged(value-i, value);
            emit resultIsReady(r);
          }
      }

        emit progresChanged( value, value);
        emit resultIsReady(r);
  }

signals:
  void progresChanged(int v, int max);
  void resultIsReady(long long result);

private:
    long long value;
};


#endif // MAINWINDOW_H
